package com.cloud;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import com.parse.endpoint.ParserEndPoint;

public class LocateDir extends JFrame {
	private JTextField dir = new JTextField(20);
	private JButton open = new JButton("Browse");
	private JButton parse = new JButton("Generate WordCloud");
	Container cp = getContentPane();
	JPanel p1 = new JPanel();
	static String dirpath = "";
	String type = "";
	int totalcount;
	static FileFilter filename;

	public LocateDir() {

		JLabel l = new JLabel("Select a Directory");
		open.addActionListener(new OpenL());
		p1.add(l, BorderLayout.PAGE_START);
		p1.add(dir);
		p1.add(open, BorderLayout.CENTER);
		cp.add(p1);
		parse.addActionListener(new ParseL());
		p1.add(parse, BorderLayout.CENTER);
		cp.add(p1);
	}

	class OpenL implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			JFileChooser chooser = new JFileChooser();
			chooser.setCurrentDirectory(new java.io.File(dirpath));
			chooser.setDialogTitle("Select Directory");
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.setAcceptAllFileFilterUsed(false);
			FileFilter javaFilter = new FileTypeFilter(".java", "Java File");
			FileFilter antlrFilter = new FileTypeFilter(".g", "Antlr File");
			chooser.addChoosableFileFilter(javaFilter);
			chooser.addChoosableFileFilter(antlrFilter);
			if (type == "" || type == "java") {
				chooser.setFileFilter(javaFilter);
			} else {
				chooser.setFileFilter(antlrFilter);
			}

			if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				dir.setText(chooser.getSelectedFile().toString());
				filename = chooser.getFileFilter();
				String fileType = chooser.getFileFilter().getDescription();
				dirpath = chooser.getSelectedFile().toString();
				if (fileType.startsWith("Java")) {
					Files f = new Files(chooser.getSelectedFile(), "java");
					totalcount = f.ReturnCount();
					System.out.println("Files found = " + totalcount);
					type = "java";
				} else if (fileType.startsWith("Antlr")) {
					Files f = new Files(chooser.getSelectedFile(), "g");
					totalcount = f.ReturnCount();
					System.out.println("Files found = " + totalcount);
					type = "g";
				}
				System.out.println("Type = " + type);
			}
		}
	}

	class ParseL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (totalcount == 0) {
				JOptionPane.showMessageDialog(cp, "No Files found!");
			} else {
				cp.setVisible(false);
				dispose();
				System.out.println("Path = " + dirpath);
				System.out.println("FileType = " + type);
				ParserEndPoint p = new ParserEndPoint();
				p.parse(dirpath, type);

			}
		}
	}

	public static void main(String[] args) {
		run(new LocateDir(), 400, 500);
	}

	public static void run(JFrame frame, int width, int height) {
		frame.setBounds(500, 100, width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
